#include "test_prereq.h"

#include "disable_warning.h"

#ifdef TEST_OPEN_FILE

#define TEST_OPEN_FILE_000
#define TEST_OPEN_FILE_001
#define TEST_OPEN_FILE_002

#define TEST_OPEN_FILE_003
#define TEST_OPEN_FILE_004
#define TEST_OPEN_FILE_005
#define TEST_OPEN_FILE_006
#define TEST_OPEN_FILE_007
#define TEST_OPEN_FILE_008
#define TEST_OPEN_FILE_009
#define TEST_OPEN_FILE_010
#define TEST_OPEN_FILE_011
#define TEST_OPEN_FILE_012
#define TEST_OPEN_FILE_013
#define TEST_OPEN_FILE_014
#define TEST_OPEN_FILE_015
#define TEST_OPEN_FILE_016
#define TEST_OPEN_FILE_017
#define TEST_OPEN_FILE_018
#define TEST_OPEN_FILE_019
#define TEST_OPEN_FILE_020
#define TEST_OPEN_FILE_021
#define TEST_OPEN_FILE_022
#define TEST_OPEN_FILE_023
#define TEST_OPEN_FILE_024

// #define TEST_OPEN_FILE_025
// #define TEST_OPEN_FILE_026
// #define TEST_OPEN_FILE_027
// #define TEST_OPEN_FILE_028
// #define TEST_OPEN_FILE_029
// #define TEST_OPEN_FILE_030
// #define TEST_OPEN_FILE_031
// #define TEST_OPEN_FILE_032

//------------------------------------------------------------------------------------------------------//
namespace{

    typedef ::std::string Str;
    const os::path& p = os::WhereProgramIsRunning();

    const os::path& work = p/"../for_test1";
    const os::path& file = work/"auto.txt";

    void CreateTestFile()
    {
        EXPECT_EQ(true,  os::XCreate(work) );
        ::std::fstream f = os::OpenFile(file);

        const bool succes = (f)? true: false;
        EXPECT_EQ(true,  succes );
        f << "1 line\n";
        f << "2 line";
    }

    void AppendTestFile()
    {
        CreateTestFile();

        ::std::fstream stream = os::OpenFile(file, eAPPEND);
        stream<<"\n3 line\n";
        stream<<"4 line";
    }

    void DeleteTestFile()
    {
        EXPECT_EQ(true, os::RemoveAll(work) );
    }


    void Scan2File(const path& scan_dir,const path& file_out)
    {
        ::std::fstream f = os::OpenFile(file_out);

        if(!f)
            return;

        os::SetPath d1,f1;
        EXPECT_EQ(true,  os::ScanDirectory(scan_dir,d1,f1,true) );

        os::SetPath d2,f2;
        for( const auto& i: d1)
            d2.emplace( canonical(i).generic_string() );
        for( const auto& i: f1)
            f2.emplace( canonical(i).generic_string() );

        f << d2;

        if(f2.size()!=0)
            f << '\n' <<"---------------------"<<endl;

        f << f2;

    }
}
//------------------------------------------------------------------------------------------------------//

#ifdef TEST_OPEN_FILE_000
TEST(os, open_file_000)
{
    CreateTestFile();
    ::std::vector<Str> content;
    ::std::fstream in(file.string(), ::std::ios::in);
    while (!in.eof())
    {
        Str line;  
        ::std::getline(in, line);
        content.push_back( line );
    }
    in.close();
    EXPECT_EQ(2,  content.size() );
    DeleteTestFile();
}
#endif // TEST_OPEN_FILE_000

#ifdef TEST_OPEN_FILE_001
TEST(os, open_file_001)
{
    AppendTestFile();
    ::std::vector<Str> content;
    ::std::fstream in(file.string(), ::std::ios::in);
    while (!in.eof())
    {
        Str line;  
        ::std::getline(in, line);
        content.push_back( line );
    }
    in.close();

    EXPECT_EQ(4,  content.size() );
    DeleteTestFile();
}
#endif // TEST_OPEN_FILE_001

#ifdef TEST_OPEN_FILE_002
TEST(os, open_file_002)
{
//     if (Exists(work))
//         RemoveAll(work);
// 
//     XCreate(work);
// 
//     Scan2File("C:/proects/external/gtest",   work/"gtest.txt");
//     Scan2File("C:/proects/external/den_lib", work/"den_lib.txt");
//     Scan2File("C:/proects/external/pqxx",    work/"pqxx.txt");
//     Scan2File("C:/proects/external/cegui",   work/"cegui.txt");
//     Scan2File("C:/proects/external/ogre",    work/"ogre.txt");
//     Scan2File("C:/proects/external/boost",   work/"boost.txt");
}
#endif // TEST_OPEN_FILE_002

#ifdef TEST_OPEN_FILE_003

#endif // TEST_OPEN_FILE_003

#ifdef TEST_OPEN_FILE_004
#endif // TEST_OPEN_FILE_004

#ifdef TEST_OPEN_FILE_005
#endif // TEST_OPEN_FILE_005

#ifdef TEST_OPEN_FILE_006
#endif // TEST_OPEN_FILE_006

#ifdef TEST_OPEN_FILE_007
#endif // TEST_OPEN_FILE_007

#ifdef TEST_OPEN_FILE_008
#endif // TEST_OPEN_FILE_008

#ifdef TEST_OPEN_FILE_009
#endif // TEST_OPEN_FILE_009

#ifdef TEST_OPEN_FILE_010
#endif // TEST_OPEN_FILE_010

#ifdef TEST_OPEN_FILE_011
#endif // TEST_OPEN_FILE_011

#ifdef TEST_OPEN_FILE_012
#endif // TEST_OPEN_FILE_012

#ifdef TEST_OPEN_FILE_013
#endif // TEST_OPEN_FILE_013

#ifdef TEST_OPEN_FILE_014
#endif // TEST_OPEN_FILE_014

#ifdef TEST_OPEN_FILE_015
#endif // TEST_OPEN_FILE_015

#ifdef TEST_OPEN_FILE_016
#endif // TEST_OPEN_FILE_016

#ifdef TEST_OPEN_FILE_017
#endif // TEST_OPEN_FILE_017

#ifdef TEST_OPEN_FILE_018
#endif // TEST_OPEN_FILE_018

#ifdef TEST_OPEN_FILE_019
#endif // TEST_OPEN_FILE_019

#ifdef TEST_OPEN_FILE_020
#endif // TEST_OPEN_FILE_020

#ifdef TEST_OPEN_FILE_021
#endif // TEST_OPEN_FILE_021

#ifdef TEST_OPEN_FILE_022
#endif // TEST_OPEN_FILE_022

#ifdef TEST_OPEN_FILE_023
#endif // TEST_OPEN_FILE_023

#ifdef TEST_OPEN_FILE_024
#endif // TEST_OPEN_FILE_024

#ifdef TEST_OPEN_FILE_025
#endif // TEST_OPEN_FILE_025

#ifdef TEST_OPEN_FILE_026
#endif // TEST_OPEN_FILE_026

#ifdef TEST_OPEN_FILE_027
#endif // TEST_OPEN_FILE_027

#ifdef TEST_OPEN_FILE_028
#endif // TEST_OPEN_FILE_029

#ifdef TEST_OPEN_FILE_030
#endif // TEST_OPEN_FILE_030

#ifdef TEST_OPEN_FILE_031
#endif // TEST_OPEN_FILE_031

#ifdef TEST_OPEN_FILE_032
#endif // TEST_OPEN_FILE_032

#endif

#include "default_warning.h"

